package cn.learn.mq.core;

import cn.learn.mq.core.pubsub.AbstractChannelMessage;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.Getter;
import lombok.Setter;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Component;
import utils.json.JsonUtil;

/**
 * @author : 翌擎科技
 * @date : 2022/7/26 16:43
 */
@AllArgsConstructor
@Slf4j
public class RedisMqTemplate {

    @Getter
    private final RedisTemplate<String, Object> redisTemplate;

    @Getter
    private String templateId;

//    @Autowired
//    private  RedisTemplate<String, Object> redisTemplate;

    public <T extends AbstractChannelMessage> void send(T message) {
        //发送消息
        log.info("发送消息message={}",JsonUtil.toJsonString(message));
        redisTemplate.convertAndSend(message.getChannel(), JsonUtil.toJsonString(message));
    }
}
